package com.example.llm.learn.springmcp;

import java.util.Map;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.ListToolsResult;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Christian Tzolov
 */

@Slf4j
public class SampleClient {

    private final McpClientTransport transport;

    public SampleClient(McpClientTransport transport) {
        this.transport = transport;
    }

    public void run() {

        McpSyncClient client = McpClient.sync(this.transport).build();

        client.initialize();

        client.ping();

        // List and demonstrate tools
        ListToolsResult toolsList = client.listTools();
//        log.info("Available Tools = " + toolsList);
//        toolsList.tools().stream().forEach(tool -> {
//            log.info("Tool: " + tool.name() + ", description: " + tool.description() + ", schema: " + tool.inputSchema());
//        });

        CallToolResult phoneResult = client.callTool(new CallToolRequest("getPhone", Map.of("brand", "苹果", "version", "14")));
        log.info("phone Response = " + phoneResult);

        CallToolResult weatherForcastResult = client.callTool(new CallToolRequest("getWeatherForecastByLocation",
                Map.of("latitude", "47.6062", "longitude", "-122.3321")));
        log.info("Weather Forcast: " + weatherForcastResult);

        CallToolResult alertResult = client.callTool(new CallToolRequest("getAlerts", Map.of("state", "NY")));
        log.info("Alert Response = " + alertResult);

        client.closeGracefully();

    }

}